package com.plian.system.service.sys.orgzation;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.StringUtil;
import com.plian.Tools.Tools;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.bean.wf.User;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.sys.orgzation.WorkFlowGroupDao;
import com.plian.system.entity.sys.WfGroup;
import com.plian.system.helper.wf.SessionHelper;
import com.plian.system.mapper.sys.WfGroupMapper;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工作流用户组服务类
 */
@Service
@Slf4j
public class WorkFlowGroupService {
    @Autowired
    private WorkFlowGroupDao workFlowGroupDao;

    @Autowired
    private OrgzationService orgzationService;

    @Autowired
    private WfGroupMapper wfGroupMapper;

    /**
     * 增加用户组
     * @param pd 请求数据
     */
    public void add(PageData pd) {
        try {
            User user = SessionHelper.getUser();
            pd.put("id", UuidUtil.get32UUID());
            LambdaQueryWrapper<WfGroup> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(WfGroup::getId, WfGroup::getName, WfGroup::getCode)
                    .isNotNull(WfGroup::getCode)
                    .apply("is_delete = 0")
                    .apply("code REGEXP '^[0-9]+$'")
                    .last("ORDER BY CAST(code AS UNSIGNED) desc,id desc LIMIT 1");
            WfGroup maxCode = wfGroupMapper.selectOne(wrapper);
            if(null == maxCode || StringUtils.isBlank(maxCode.getCode())){
                pd.put("code","001");
            }else {
                String max = maxCode.getCode();
                int numericCode = Integer.parseInt(max);
                numericCode++;
                pd.put("code",String.format("%03d", numericCode));
            }
            workFlowGroupDao.insert(pd);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult("往数据库插入工作流用户组信息时失败！"));
        }
    }

    public List<PageData> list() {
        try {
            return workFlowGroupDao.list();
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult("获取所有用户组时失败！"));
        }
    }
    public ListResult<PageData> listPage(PageData pd) {
        try {
            String keyword=pd.getString("keyword");
            if(Tools.notEmpty(keyword)){
                keyword= keyword.replaceAll("_", "/_");
                pd.put("keyword",keyword.trim());
            }else{
                Object keywords=pd.get("keywords");
                if(keywords!=null){
                    List<String> keyList=(List<String>)keywords;
                    if(keyList!=null&&keyList.size()>0){
                        String get=keyList.get(0);
                        if(StringUtil.isNotBlank(get)){
                            get= get.replaceAll("_", "/_");
                            pd.put("keyword",get.trim());
                        }
                    }
                }
            }
             Page page=new Page(true,pd);
             List<PageData> l=workFlowGroupDao.listPage(page);
             ListResult listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(l);
            listResult.setTotal(page.getTotal());
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult("获取所有用户组时失败！"));
        }
    }
    public ListResult<PageData> listShowPage(PageData pd) {
        try {
            String keyword=pd.getString("keyword");
            if(Tools.notEmpty(keyword)){
                keyword= keyword.replaceAll("_", "/_");
                pd.put("keyword",keyword.trim());
            }
            String orgId = pd.getString("orgId");
            if (Tools.notEmpty(orgId)){
                pd.put("grade", 1);
            }

            Page page=new Page(false,pd);
            List<PageData> l = workFlowGroupDao.listShowPage(page);
            ListResult listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(l);
            listResult.setTotal(page.getTotal());
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult("获取所有用户组时失败！"));
        }
    }

    public void delete(PageData pd) {
        try {
            workFlowGroupDao.delete(pd);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult("数据库删除岗位类型时失败！"));
        }
    }

    public void update(PageData pd) {
        try {
            workFlowGroupDao.update(pd);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult("数据库更新岗位类型时失败！"));
        }
    }
}
